package cn.sumpu.app.guoguojie.web.mvc.family;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.sumpu.app.guoguojie.biz.DoingBiz;
import cn.sumpu.app.guoguojie.constant.Expression;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Ext.PageModel;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

@Controller
public class DoingController {

	@Autowired
	private DoingBiz doingBiz;
	
	//
	@RequestMapping(value = "/doing.htm", method = RequestMethod.GET)
	public String enterWeiBo(HttpServletRequest request) {
		try {
			Member member = (Member) request.getSession().getAttribute("member");
			Doing doing = doingBiz.getDoingByMember(member.getIdentifier());
			Map<String, String> expressionMap = Expression.cache;
			request.setAttribute("expressionMap", expressionMap);
			request.setAttribute("doing", doing);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "doing/doing";
	}
	
	@RequestMapping(value = "/saveDoing.htm", method = RequestMethod.POST)
	public String saveWeiBo(Doing doing,HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		try {
			Member member = (Member) request.getSession().getAttribute("member");
			doing.setMember(member.getIdentifier());
			doing.setPostIp(request.getRemoteAddr());
			doing.setFamily(member.getFamily());
			doingBiz.insert(doing);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	@RequestMapping(value = "/familyDoingAll.htm")
	public String familyDoingAll(Integer page,HttpServletRequest request,HttpServletResponse response) {
		Member member = (Member) request.getSession().getAttribute("member");
		if(page==null){
			page = 1;
		}
		try {
			PageModel pageModel = doingBiz.getDoingModel(member.getFamily(), page);
			request.setAttribute("pageModel", pageModel);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return "doing/familyDoingAll";
	}
	
	@RequestMapping(value = "/familyDoing.htm")
	public String familyDoing(Integer page,HttpServletRequest request,HttpServletResponse response) {
		Member member = (Member) request.getSession().getAttribute("member");
		if(page==null){
			page = 1;
		}
		try {
			PageModel pageModel = doingBiz.getDoingModel(member.getFamily(), page);
			request.setAttribute("pageModel", pageModel);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return "doing/familyDoing";
	}
	
	@RequestMapping(value = "/deleteDoing.htm", method = RequestMethod.POST)
	public String deleteDoing(String doingId,HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		Member member = (Member) request.getSession().getAttribute("member");
		String webAppPath = request.getSession(true).getServletContext()
		.getRealPath("/");
		try {
			doingBiz.deleteDoing(member,doingId,webAppPath);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().print("error");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} 
		return null;
	}
	
	
	@RequestMapping(value = "/deleteDoingImg.htm", method = RequestMethod.POST)
	public String deleteDoingImg(Doing doing,HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		String webAppPath = request.getSession(true).getServletContext()
		.getRealPath("/");
		try {
			doingBiz.deleteDoingImg(doing, webAppPath);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().print("error");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} 
		return null;
	}
	
	/**
	 * 转发微博页面
	 * */
	@RequestMapping(value = "/doingForward.htm")
	public String doingForward(String doingId,String feedIdType,String groupClassify, String familyId,String classify,HttpServletRequest request,HttpServletResponse response) {
		request.setAttribute("doingId", doingId); 
		request.setAttribute("feedIdType", feedIdType);
		request.setAttribute("groupClassify", groupClassify);
		request.setAttribute("familyId", familyId);
		request.setAttribute("classify", classify);
		request.setAttribute("doorplate", familyId);
		request.setAttribute("feedGroup", groupClassify);
		return "doing/doingForward"; 
	}
	
	/**
	 * 转发微博保存
	 * */
	@RequestMapping(value = "/doingForwardSave.htm")
	public String doingForwardSave(String doingId,String feedIdType,String content,HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		Member member = (Member) request.getSession().getAttribute("member");
		try {
			doingBiz.doingForward( doingId, content, member);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null; 
	}

}
